#include <windows.h>
#include <string.h>
#include <stdio.h>
#include "resource.h"
#include "resource1.h"

char a[6][3]={"A","B","C","D","E","F"},mat[16][16];
int n=0,ind=0,sind=0,nrm=0,start=0,intrgrf=0,z=0,viz[12],adiac[6][6]={0};
struct muchie
{
	char s[32];
	char u[3];
	char v[3];
	int p[18];
};
muchie v[12]={0},arb[5]={0},elim[12]={0};
HPEN hPen;
HDC hDC;
HWND btnext=NULL;

#include "sortare.h"
void sortare_bubble(int);

#include"costuri_fisier.h"
int extract_file(HWND);

#include"gestionare_sesiune_anterioara.h"
int write_session();
int read_session();

#include"kruskal.h"
void kruskal(int);

#include"verif_functionalitate.h"
int verif_inceput_arb_cost_min();
int verif_conex(int,HWND);
void reset();

BOOL CALLBACK dialog1(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:
             return TRUE;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
     			case IDCANCEL:
					EndDialog(hwnd,IDCANCEL);
					return FALSE;
                case IDOK :
					 for(int i=0;i<12;i++)
		                 strcpy(mat[i],"");
					 GetDlgItemText(hwnd,IDC_EDIT1,mat[0],4);
					 GetDlgItemText(hwnd,IDC_EDIT2,mat[1],4);
					 GetDlgItemText(hwnd,IDC_EDIT4,mat[2],4);
					 GetDlgItemText(hwnd,IDC_EDIT5,mat[3],4);
					 GetDlgItemText(hwnd,IDC_EDIT6,mat[4],4);
					 GetDlgItemText(hwnd,IDC_EDIT7,mat[5],4);
					 GetDlgItemText(hwnd,IDC_EDIT3,mat[6],4);
					 GetDlgItemText(hwnd,IDC_EDIT8,mat[7],4);
					 GetDlgItemText(hwnd,IDC_EDIT9,mat[8],4);
					 GetDlgItemText(hwnd,IDC_EDIT10,mat[9],4);
					 GetDlgItemText(hwnd,IDC_EDIT11,mat[10],4);
					 GetDlgItemText(hwnd,IDC_EDIT12,mat[11],4);
					 if (write_session()==0)
						   MessageBox(hwnd, "Nu s-a putut scrie fisierul \"last_data.in\" pentru sesiunea curenta", "Eroare",MB_OK | MB_ICONINFORMATION);
					 EndDialog(hwnd,IDOK);
                break; 
            }
        break;
        default:return FALSE;
    }
    return TRUE;
}

BOOL CALLBACK dialog2 (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:
        return TRUE;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
			case IDC_BUTTON1:
			     if (ind==nrm)
					{if (!intrgrf)
				            {MessageBox(hwnd,"Nu a-ti introdus nici un graf !","Alert !", MB_OK);
				             break;
				            }
				     if(!verif_conex(0,hwnd))
					 {MessageBox(hwnd,"Graful introdus nu este conex.\n\rIntroduceti un alt graf!","Alert !", MB_OK);
				             break;
					 }

					 MessageBox(hwnd,"Algoritmul s-a terminat . Introduceti alte date pentru a incepe o noua demonstrare !","Alert !", MB_OK);
					 hPen = CreatePen(PS_SOLID, 1, RGB(0,255,0));
                     SelectObject(hDC, hPen);
					 SetTextColor(hDC,RGB(153,51,51));
				    for(int j=0;j<12;j++)
     					if((viz[j]==0)&&(strcmp(elim[j].s,"-1")))
				           MoveToEx(hDC,elim[j].p[0],elim[j].p[1],NULL),
						   LineTo(hDC,elim[j].p[2],elim[j].p[3]),
						   TextOut(hDC,elim[j].p[4],elim[j].p[5],elim[j].s,strlen(elim[j].s));

					Ellipse(hDC, 180, 280,240,340);
					TextOut(hDC, 204, 302,a[3],2);//jj
		   
					Ellipse(hDC, 180, 20,240,80);
					TextOut(hDC, 204, 43,a[0],2);//ss
		
					Ellipse(hDC, 60, 80,120,140);
					TextOut(hDC, 84, 102,a[1],2);//s stg

					Ellipse(hDC, 60, 220,120,280);
					TextOut(hDC, 84, 242,a[2],2);//j stg
		 
					Ellipse(hDC, 300, 80,360,140);
					TextOut(hDC, 323, 102,a[5],2);//s dr

					Ellipse(hDC, 300, 220,360,280);
					TextOut(hDC, 323, 242,a[4],2);//j dr

				  TextOut(hDC, 30, 370,"FIGURA DE MAI SUS REPREZINTA ARBORELE DE COST MINIM CAUTAT",strlen("FIGURA DE MAI SUS REPREZINTA ARBORELE DE COST MINIM CAUTAT"));
				     break;
				    }
				 if(!verif_conex(0,hwnd))
					 {MessageBox(hwnd,"Graful introdus nu este conex.\n\rIntroduceti un alt graf!","Alert !", MB_OK);
				             break;
					 }
				 if(verif_inceput_arb_cost_min())
				 {
					 MessageBox(hwnd,"Graful introdus coincide cu arborele de cost minim cautat!","Alert !", MB_OK);
						 hPen = CreatePen(PS_SOLID, 1, RGB(0,255,0));
                         SelectObject(hDC, hPen);
					     SetTextColor(hDC,RGB(153,51,51));
				for(int j=0;j<12;j++)
					if((viz[j]==0)&&(strcmp(elim[j].s,"-1")))
				    MoveToEx(hDC,elim[j].p[0],elim[j].p[1],NULL),
					LineTo(hDC,elim[j].p[2],elim[j].p[3]),
					TextOut(hDC,elim[j].p[4],elim[j].p[5],elim[j].s,strlen(elim[j].s));

					Ellipse(hDC, 180, 280,240,340);
					TextOut(hDC, 204, 302,a[3],2);//jj
		   
					Ellipse(hDC, 180, 20,240,80);
					TextOut(hDC, 204, 43,a[0],2);//ss
		
					Ellipse(hDC, 60, 80,120,140);
					TextOut(hDC, 84, 102,a[1],2);//s stg

					Ellipse(hDC, 60, 220,120,280);
					TextOut(hDC, 84, 242,a[2],2);//j stg
		 
					Ellipse(hDC, 300, 80,360,140);
					TextOut(hDC, 323, 102,a[5],2);//s dr

					Ellipse(hDC, 300, 220,360,280);
					TextOut(hDC, 323, 242,a[4],2);//j dr

				 TextOut(hDC, 30, 370,"FIGURA DE MAI SUS REPREZINTA ARBORELE DE COST MINIM CAUTAT",strlen("FIGURA DE MAI SUS REPREZINTA ARBORELE DE COST MINIM CAUTAT"));
				 ind=5;
				 break;
				 }
				 if (!start)
				     {kruskal(nrm);
				      start=1;
				      }
                if (!sind)
				     { 
					   hPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 255));
                       SelectObject(hDC, hPen);
					   SetTextColor(hDC,  RGB(0, 255, 255));
					   Ellipse(hDC,v[ind].p[0],v[ind].p[1],v[ind].p[2],v[ind].p[3]),TextOut(hDC,v[ind].p[4],v[ind].p[5],v[ind].u,strlen(v[ind].u)),
			           Ellipse(hDC,v[ind].p[6], v[ind].p[7],v[ind].p[8],v[ind].p[9]),TextOut(hDC,v[ind].p[10],v[ind].p[11],v[ind].v,strlen(v[ind].v)),
			           MoveToEx(hDC,v[ind].p[12],v[ind].p[13],NULL),LineTo(hDC,v[ind].p[14],v[ind].p[15]),TextOut(hDC,v[ind].p[16],v[ind].p[17],v[ind].s,strlen(v[ind].s));
				 	   TextOut(hDC,v[ind].p[4]+320,v[ind].p[11],"LATURA CURENTA",strlen("LATURA CURENTA"));
					   sind=1;
					   break;
					    }

				else {if(!strcmp(arb[z].u,v[ind].u) && !strcmp(arb[z].v,v[ind].v) && z<5)
				          {z++;
					       hPen = CreatePen(PS_SOLID, 1, RGB(0,204,51));
                           SelectObject(hDC, hPen);
					      SetTextColor(hDC,  RGB(0,204,51));
			              Ellipse(hDC,v[ind].p[0],v[ind].p[1],v[ind].p[2],v[ind].p[3]),TextOut(hDC, v[ind].p[4],v[ind].p[5],v[ind].u,strlen(v[ind].u)),
                		  Ellipse(hDC,v[ind].p[6], v[ind].p[7],v[ind].p[8],v[ind].p[9]),TextOut(hDC,v[ind].p[10],v[ind].p[11],v[ind].v,strlen(v[ind].v)),
			              MoveToEx(hDC,v[ind].p[12],v[ind].p[13],NULL),LineTo(hDC,v[ind].p[14],v[ind].p[15]),TextOut(hDC,v[ind].p[16],v[ind].p[17],v[ind].s,strlen(v[ind].s));
				  
			              TextOut(hDC,v[ind].p[4]+320,v[ind].p[11],"LATURA ESTE ACCEPTATA",strlen("LATURA ESTE ACCEPTATA"));
				          }
				       else
				          {
					       hPen = CreatePen(PS_SOLID, 1, RGB(255,61,61));
                           SelectObject(hDC, hPen);
					       SetTextColor(hDC,RGB(255,61,61) );
			               Ellipse(hDC,v[ind].p[0],v[ind].p[1],v[ind].p[2],v[ind].p[3]),TextOut(hDC, v[ind].p[4],v[ind].p[5],v[ind].u,strlen(v[ind].u)),
			               Ellipse(hDC,v[ind].p[6], v[ind].p[7],v[ind].p[8],v[ind].p[9]),TextOut(hDC,v[ind].p[10],v[ind].p[11],v[ind].v,strlen(v[ind].v)),
			               MoveToEx(hDC,v[ind].p[12],v[ind].p[13],NULL),LineTo(hDC,v[ind].p[14],v[ind].p[15]),TextOut(hDC,v[ind].p[16],v[ind].p[17],v[ind].s,strlen(v[ind].s));
			               if(strcmp(v[ind].s,""))
			               TextOut(hDC,v[ind].p[4]+320,v[ind].p[11],"LATURA ESTE EXCLUSA",strlen("LATURA ESTE EXCLUSA"));
				           
				        for(int j=0;j<12;j++)
					          if(!strcmp(elim[j].u,v[ind].u) && !strcmp(elim[j].v,v[ind].v))
					             {
						          viz[j]++;
						          hPen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
                                  SelectObject(hDC, hPen);
					              SetTextColor(hDC,RGB(255,255,255));
					              MoveToEx(hDC,elim[j].p[0],elim[j].p[1],NULL),LineTo(hDC,elim[j].p[2],elim[j].p[3]),TextOut(hDC,elim[j].p[4],elim[j].p[5],elim[j].s,strlen(elim[j].s));
					            }  
				       }
					  
					  ind++;
				      sind=0;
                      break;
				     }
				

			break;
			}
			break;
		
        default:return FALSE;
    }
    return TRUE;
}	
BOOL CALLBACK dialog3(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:
             return TRUE;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK :
					 EndDialog(hwnd,IDOK);
                break; 
            }
        break;
        default:return FALSE;
    }
    return TRUE;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   PAINTSTRUCT Ps;
   int flag,ret;
   n=0;        
		
   switch(msg)
    {case WM_CREATE:
          btnext=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_FORMVIEW),hwnd,dialog2);
               if(btnext!=NULL)
			      ShowWindow(btnext,SW_SHOW);
				else
                  MessageBox(hwnd, "Butonul \"Next step\" nu s-a putut crea !","Error !", MB_OKCANCEL| MB_ICONWARNING);
	 break;
     case WM_CLOSE:
			if( MessageBox(hwnd, "Are you sure ?","Alert !", MB_OKCANCEL| MB_ICONWARNING)==IDOK)
            DestroyWindow(hwnd);
     break;
		
     case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
     break;
		    
     case WM_COMMAND:
            switch(LOWORD(wParam))
            {
			   case ID_ADD_KEYBOARD:
				   z=ind=sind=start=0;intrgrf=1;
				   for(int j=0;j<12;j++)
				   {
     					viz[j]=0;
						for(int i=0;i<6;i++)
							adiac[j/2][i]=0;
				   }

				   reset();
				   flag = DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG2),hwnd,dialog1);
				   if(flag==IDOK)
				   {
		             ShowWindow(hwnd,SW_PARENTOPENING);
		             ShowWindow(hwnd,SW_NORMAL);
				   }
				 else 
					 if(flag == -1)
					 {
                       MessageBox(hwnd, "Initializare esuata!", "Eroare",MB_OK | MB_ICONINFORMATION);
					 }
			   break;
			   case ID_ADD_FILE:
				   z=ind=sind=start=0;intrgrf=1;
				    for(int j=0;j<12;j++)
				   {
     					viz[j]=0;
						for(int i=0;i<6;i++)
							adiac[j/2][i]=0;
				   }
				   reset();
				   if(extract_file(hwnd))
				   {
				    if(write_session()==0)
						   MessageBox(hwnd, "Nu s-a putut scrie fisierul \"last_data.in\" pentru sesiunea curenta", "Eroare",MB_OK | MB_ICONINFORMATION);
				   ShowWindow(hwnd,SW_PARENTOPENING),
		           ShowWindow(hwnd,SW_NORMAL);
				   }
				   else
					   MessageBox(hwnd, "Fisierul \"in.txt\" nu exista!\n\rCreati unul in directorul unde se afla executabilul dupa modelul:\n   COST A-B:11\n   COST A-C:17\n   COST A-E:23\n   COST A-F:18\n   COST B-C:15\n   COST B-D:32\n   COST B-F:25\n   COST C-D:47\n   COST C-E:45\n   COST D-E:39\n   COST D-F:54\n   COST E-F:21\n\nAcolo unde nu vreti sa existe cost pe o muchie treceti la randul urmator dupa \":\"ex:\n   COST A-B:23\n   COST A-C:\n   COST A-D:45\n", "Eroare",MB_OK | MB_ICONINFORMATION);
			   break;
			   case ID_OPTIONS_OPENPREVIOUSSESSION:
				   z=ind=sind=start=0;intrgrf=1;
				   for(int j=0;j<12;j++)
				   {
     					viz[j]=0;
						for(int i=0;i<6;i++)
							adiac[j/2][i]=0;
				   }
				   reset();
				   if(read_session())
					  ShowWindow(hwnd,SW_PARENTOPENING),
		              ShowWindow(hwnd,SW_NORMAL);
				   else
                       MessageBox(hwnd, "Nu exista fisierul \"last_data.in\" pentru sesiunea anterioara!", "Eroare",MB_OK | MB_ICONINFORMATION);
               break;
			   case ID_INFO_HELP:
				    DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG1),hwnd,dialog3);
						 break;
			   case ID_OPTIONS_EXIT:
			  		   if( MessageBox(hwnd, "Are you sure ?","Alert !", MB_OKCANCEL| MB_ICONWARNING)==IDOK)
                       DestroyWindow(hwnd);
		        break;
			   case ID_INFO_DEVELOPERS:
				        MessageBox(hwnd,"Acest program a fost realizat de :\n      Student fruntas Coca Mihai\n      Student fruntas Lupascu Alexandru","Dezvoltatori",MB_OK);
						break;
			   case ID_INFO_DOCUMENTDETESTARE:
				       ShellExecute(hwnd,"open","DOCUMENT DE TESTARE.pdf",0,0,SW_SHOWNORMAL);
					   break;
			   case ID_INFO_MANUALDETESTARE:
				      ShellExecute(hwnd,"open","MANUAL.pdf",0,0,SW_SHOWNORMAL);
					   break;
			}
	  break;
      case WM_PAINT:
           hDC = BeginPaint(hwnd, &Ps);
		   SetBkColor(hDC, RGB(255,255,255));
		   SetTextColor(hDC,  RGB(0, 0, 128)); 
		   MoveToEx(hDC,192,286,NULL);
		   hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
           SelectObject(hDC, hPen);

		   //v-str in care salvam datele pentru laturile afisate lateral in fereastra.
		   //elim-str in care salvam coordonatele laturilor pe care le vom elimina

		   if(strcmp(mat[5],""))
		    {adiac[1][3]=adiac[3][1]=atoi(mat[5]);
		     MoveToEx(hDC,192,286,NULL);
		     LineTo(hDC,80,102);//s stg
			 TextOut(hDC, 140, 200,mat[5],strlen(mat[5]));//leg b-d

		     strcpy(v[n].s,mat[5]);
	         strcpy(v[n].u,a[1]);
	         strcpy(v[n].v,a[3]);
	         n++;

			 elim[5].p[0]=192;elim[5].p[1]=286;elim[5].p[2]=80;elim[5].p[3]=102;
			 strcpy(elim[5].u,a[1]);
			 strcpy(elim[5].v,a[3]);
			 strcpy(elim[5].s,mat[5]);elim[5].p[4]=140;elim[5].p[5]=200;
		    }
	       if(strcmp(mat[6],""))
		    {adiac[1][5]=adiac[5][1]=atoi(mat[6]);
		    MoveToEx(hDC,80,102,NULL);
		    LineTo(hDC,323,102);//s dr
			 TextOut(hDC, 198, 96,mat[6],strlen(mat[6]));//leg b-f
  
		    strcpy(v[n].s,mat[6]);
		    strcpy(v[n].u,a[1]);
		    strcpy(v[n].v,a[5]);
		    n++;

			elim[6].p[0]=80;elim[6].p[1]=102;elim[6].p[2]=323;elim[6].p[3]=102;
			strcpy(elim[6].u,a[1]);
			strcpy(elim[6].v,a[5]);
			strcpy(elim[6].s,mat[6]);elim[6].p[4]=198;elim[6].p[5]=96;
		    }
		   if(strcmp(mat[11],""))
		    {adiac[4][5]=adiac[5][4]=atoi(mat[11]);
		    MoveToEx(hDC,323,102,NULL);
		    LineTo(hDC,323,242);//j dr
			 TextOut(hDC, 316, 172,mat[11],strlen(mat[11]));//leg e-f

		    strcpy(v[n].s,mat[11]);
		    strcpy(v[n].u,a[4]);
		    strcpy(v[n].v,a[5]);
		    n++;

			elim[11].p[0]=323;elim[11].p[1]=102;elim[11].p[2]=323;elim[11].p[3]=242;
			strcpy(elim[11].u,a[4]);
			strcpy(elim[11].v,a[5]);
			strcpy(elim[11].s,mat[11]);elim[11].p[4]=316;elim[11].p[5]=172;
		    }
		   if(strcmp(mat[8],""))
		    {adiac[2][4]=adiac[4][2]=atoi(mat[8]);
		    MoveToEx(hDC,323,242,NULL);
		    LineTo(hDC,80,242);//j stg
			TextOut(hDC, 198, 236,mat[8],strlen(mat[8]));//leg c-e

		    strcpy(v[n].s,mat[8]);
		    strcpy(v[n].u,a[2]);
		    strcpy(v[n].v,a[4]);
		    n++;

			elim[8].p[0]=323;elim[8].p[1]=242;elim[8].p[2]=80;elim[8].p[3]=242;
			strcpy(elim[8].u,a[2]);
			strcpy(elim[8].v,a[4]);
			strcpy(elim[8].s,mat[8]);elim[8].p[4]=198;elim[8].p[5]=236;
		    }
		   if(strcmp(mat[4],""))
		    {adiac[1][2]=adiac[2][1]=atoi(mat[4]);
		    MoveToEx(hDC,80,242,NULL);
		    LineTo(hDC,80,102);//s stg
			TextOut(hDC, 74, 172,mat[4],strlen(mat[4]));//leg b-c

		    strcpy(v[n].s,mat[4]);
		    strcpy(v[n].u,a[1]);
		    strcpy(v[n].v,a[2]);
		    n++;

			elim[4].p[0]=80;elim[4].p[1]=242;elim[4].p[2]=80;elim[4].p[3]=102;
			strcpy(elim[4].u,a[1]);
			strcpy(elim[4].v,a[2]);
			strcpy(elim[4].s,mat[4]);elim[4].p[4]=74;elim[4].p[5]=172;
		    }
		   if(strcmp(mat[0],""))
		    {adiac[1][0]=adiac[0][1]=atoi(mat[0]);
		    MoveToEx(hDC,80,102,NULL);
		    LineTo(hDC,204,43);//ss
			TextOut(hDC, 134, 66,mat[0],strlen(mat[0]));//leg a-b

		    strcpy(v[n].s,mat[0]);
		    strcpy(v[n].u,a[0]);
		    strcpy(v[n].v,a[1]);
		    n++;

			elim[0].p[0]=80;elim[0].p[1]=102;elim[0].p[2]=204;elim[0].p[3]=43;
			strcpy(elim[0].u,a[0]);
		    strcpy(elim[0].v,a[1]);
			strcpy(elim[0].s,mat[0]);elim[0].p[4]=134;elim[0].p[5]=66;
		    }
		   if(strcmp(mat[3],""))
		    {adiac[0][5]=adiac[5][0]=atoi(mat[3]);
		    MoveToEx(hDC,204,43,NULL);
		    LineTo(hDC,323,102);//s dr
			TextOut(hDC, 260, 66,mat[3],strlen(mat[3]));//leg a-f

		    strcpy(v[n].s,mat[3]);
		    strcpy(v[n].u,a[0]);
		    strcpy(v[n].v,a[5]);
		    n++;

			elim[3].p[0]=204;elim[3].p[1]=43;elim[3].p[2]=323;elim[3].p[3]=102;
			strcpy(elim[3].u,a[0]);
			strcpy(elim[3].v,a[5]);
			strcpy(elim[3].s,mat[3]);elim[3].p[4]=260;elim[3].p[5]=66;
		    }
		  if(strcmp(mat[10],""))
		    {adiac[5][3]=adiac[3][5]=atoi(mat[10]);
		    MoveToEx(hDC,323,102,NULL);
		    LineTo(hDC,227,286);//jj
			TextOut(hDC, 262, 200,mat[10],strlen(mat[10]));//leg d-f

		    strcpy(v[n].s,mat[10]);
		    strcpy(v[n].u,a[3]);
		    strcpy(v[n].v,a[5]);
		    n++;

			elim[10].p[0]=323;elim[10].p[1]=102;elim[10].p[2]=227;elim[10].p[3]=286;
			strcpy(elim[10].u,a[3]);
			strcpy(elim[10].v,a[5]);
			strcpy(elim[10].s,mat[10]);elim[10].p[4]=262;elim[10].p[5]=200;
		   }
		  if(strcmp(mat[9],""))
		   {adiac[3][4]=adiac[4][3]=atoi(mat[9]);
		    MoveToEx(hDC,235,296,NULL);//mutare punct de plecare
		    LineTo(hDC,323,242);//j dr
			 TextOut(hDC, 258, 270,mat[9],strlen(mat[9]));//leg d-e

			strcpy(v[n].s,mat[9]);
			strcpy(v[n].u,a[3]);
			strcpy(v[n].v,a[4]);
			n++;

			elim[9].p[0]=235;elim[9].p[1]=296;elim[9].p[2]=323;elim[9].p[3]=242;
			strcpy(elim[9].u,a[3]);
			strcpy(elim[9].v,a[4]);
			strcpy(elim[9].s,mat[9]);elim[9].p[4]=258;elim[9].p[5]=270;
	    	}
	     if(strcmp(mat[2],""))
		   {adiac[0][4]=adiac[4][0]=atoi(mat[2]);
			MoveToEx(hDC,323,242,NULL);
			LineTo(hDC,204,43);//ss
			TextOut(hDC, 253, 126,mat[2],strlen(mat[2]));//leg a-e

			strcpy(v[n].s,mat[2]);
			strcpy(v[n].u,a[0]);
			strcpy(v[n].v,a[4]);
			n++;

			elim[2].p[0]=323;elim[2].p[1]=242;elim[2].p[2]=204;elim[2].p[3]=43;
			strcpy(elim[2].u,a[0]);
			strcpy(elim[2].v,a[4]);
			strcpy(elim[2].s,mat[2]);elim[2].p[4]=253;elim[2].p[5]=126;
		   }
		 if(strcmp(mat[1],""))
		  {adiac[0][2]=adiac[2][0]=atoi(mat[1]);
		    MoveToEx(hDC,204,43,NULL);
		    LineTo(hDC,80,242);//j stg
		    TextOut(hDC, 142, 126,mat[1],strlen(mat[1]));//leg a-c

			strcpy(v[n].s,mat[1]);
			strcpy(v[n].u,a[0]);
			strcpy(v[n].v,a[2]);
			n++;

			elim[1].p[0]=204;elim[1].p[1]=43;elim[1].p[2]=80;elim[1].p[3]=242;
			strcpy(elim[1].u,a[0]);
			strcpy(elim[1].v,a[2]);
			strcpy(elim[1].s,mat[1]);elim[1].p[4]=142;elim[1].p[5]=126;
		   }
		if(strcmp(mat[7],""))
		 {adiac[2][3]=adiac[3][2]=atoi(mat[7]);
		   MoveToEx(hDC,80,242,NULL);
		   LineTo(hDC,184,298);//end--jj
		    TextOut(hDC, 138, 270,mat[7],strlen(mat[7]));//leg c-d

		   strcpy(v[n].s,mat[7]);
		   strcpy(v[n].u,a[2]);
		   strcpy(v[n].v,a[3]);
		   n++;

		    elim[7].p[0]=80;elim[7].p[1]=242;elim[7].p[2]=184;elim[7].p[3]=298;
			strcpy(elim[7].u,a[2]);
			strcpy(elim[7].v,a[3]);
			strcpy(elim[7].s,mat[7]);elim[7].p[4]=138;elim[7].p[5]=270;
		 }  
		   nrm=n;
		if(n)
		 {
	       sortare_bubble(n); 
		   for(int i=0;i<n;i++)
			   Ellipse(hDC, 540, 10+45*(i),580,50+45*(i)),v[i].p[0]=540,v[i].p[1]=10+45*i,v[i].p[2]=580,v[i].p[3]=50+45*i,TextOut(hDC, 556,22+45*(i),v[i].u,strlen(v[i].u)),v[i].p[4]=556,v[i].p[5]=22+45*i,
			   Ellipse(hDC, 660+10*i, 10+45*(i),700+10*i,50+45*(i)),v[i].p[6]=660+10*i,v[i].p[7]=10+45*i,v[i].p[8]=700+10*i,v[i].p[9]=50+45*i,TextOut(hDC, 676+10*i,22+45*(i),v[i].v,strlen(v[i].v)),v[i].p[10]=676+10*i,v[i].p[11]=22+45*i,
			   MoveToEx(hDC,580,30+45*i,NULL),v[i].p[12]=580,v[i].p[13]=30+45*i,LineTo(hDC,660+10*i,30+45*i),v[i].p[14]=660+10*i,v[i].p[15]=30+45*i,TextOut(hDC,616+4*i,22+45*i,v[i].s,strlen(v[i].s)),v[i].p[16]=616+4*i,v[i].p[17]=22+45*i;
		       
		 }

		   Ellipse(hDC, 180, 280,240,340);
		   TextOut(hDC, 204, 302,a[3],2);//jj
		   
		   Ellipse(hDC, 180, 20,240,80);
		   TextOut(hDC, 204, 43,a[0],2);//ss
		
		   Ellipse(hDC, 60, 80,120,140);
		   TextOut(hDC, 84, 102,a[1],2);//s stg

		   Ellipse(hDC, 60, 220,120,280);
		   TextOut(hDC, 84, 242,a[2],2);//j stg
		 
		   Ellipse(hDC, 300, 80,360,140);
		   TextOut(hDC, 323, 102,a[5],2);//s dr

		   Ellipse(hDC, 300, 220,360,280);
		   TextOut(hDC, 323, 242,a[4],2);//j dr
           EndPaint(hwnd, &Ps);
        break; 
     default:return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    static char szAppName[] = "meniul meu principal";

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInst;
	wc.hIcon  =      LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
	wc.hCursor       = LoadCursor(hInst,IDC_HAND);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
	wc.lpszClassName = szAppName;
    wc.hIconSm  =    (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, 0);
	
	if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
	
    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
		WS_EX_OVERLAPPEDWINDOW,
                          szAppName,
                          "Demonstrator grafic pentru algoritmul lui Kruskal",
                          WS_OVERLAPPEDWINDOW  | WS_VISIBLE,
						  100,
                          100,
                          1100,
                          620,
                          NULL,
                          NULL,
                          hInst,
                          NULL);
	if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    ShowWindow(hwnd,nCmdShow);
    UpdateWindow(hwnd);
	
    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        DispatchMessage(&Msg);
        TranslateMessage(&Msg);
    }
    return static_cast<int>(Msg.wParam);
}